package listbyorder.access001_100.test90;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/5 13:57
 */
public class Solution1 {

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        if (nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        dfs(res, nums, 0, new ArrayDeque<Integer>());
        return res;
    }

    private void dfs(List<List<Integer>> res, int[] nums, int s, ArrayDeque<Integer> path) {
        res.add(new ArrayList<>(path));
        for (int i = s; i < nums.length; i++) {
            if(i > s && nums[i] == nums[i - 1]){
                continue;
            }
            path.addLast(nums[i]);
            dfs(res, nums, i + 1, path);
            path.removeLast();
        }
    }
}
